<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .modal {
            display: none;
            position: fixed;
            z-index: 1;
            left: 0;
            top: 0;
            width: 400px;
            height: 500px;
            overflow: auto;
            background-color: rgba(0, 0, 0, 0.4);
        }

        .modal-content {
            background-color: #fefefe;
            margin: 15% auto;
            padding: 20px;
            border: 1px solid #888;
            width: 80%;
        }
    </style>
</head>

<body>
    <button id="openModal">打开模态框</button>
    <div id="modal" class="modal">
        <div class="modal-content">
        </div>
    </div>

    <script>
        const modal = document.getElementById('modal');
        const btn = document.getElementById('openModal');

        btn.onclick = function () {
            modal.style.display = "block";
        }

        // 点击模态框外部关闭
        window.onclick = function (event) {
            if (event.target == modal) {
                modal.style.display = "none";
            }
        }

    </script>

</body>

</html>